﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using AutoLoL.Controls;
using AutoLoL.Dialogs;
using AutoLoL.Entities;
using AutoLoL.Logic;

namespace AutoLoL.Tabs
{
    /// <summary>
    /// Interaction logic for AutoChatTab.xaml
    /// </summary>
    public partial class AutoChatTab : UserControl
    {
        public AutoChatTab()
        {
            InitializeComponent();
            LoadItems();
        }

        private void LoadItems()
        {
            ItemsStackPanel.Children.Clear();

            List<ChatItem> items = UserSettings.Instance.ChatItems;

            // Display items
            foreach (ChatItem item in items)
            {
                ChatItemControl itemControl = new ChatItemControl(item.Index, item.Title);
                ItemsStackPanel.Children.Add(itemControl);
                AddItemControlChildrenRecursive(item, itemControl);
            }
        }

        private void AddItemControlChildrenRecursive(ChatItem parent, ChatItemControl parentControl)
        {
            if (parent.Children != null)
            {
                List<ChatItemControl> children = new List<ChatItemControl>();

                foreach (ChatItem item in parent.Children)
                {
                    ChatItemControl itemControl = new ChatItemControl(item.Index, item.Title);
                    children.Add(itemControl);
                    AddItemControlChildrenRecursive(item, itemControl);
                }

                parentControl.Children = children;
            }
        }

        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            EditButton.Visibility = Visibility.Collapsed;
            PrintButton.Visibility = Visibility.Collapsed;
            SaveButton.Visibility = Visibility.Visible;
            CancelButton.Visibility = Visibility.Visible;

            foreach (ChatItemControl c in ItemsStackPanel.Children)
            {
                c.IsEditable = true;
            }
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            List<ChatItem> items = new List<ChatItem>();
            foreach (ChatItemControl itemControl in ItemsStackPanel.Children)
            {
                ChatItem item = new ChatItem(itemControl.Index, itemControl.Title);
                items.Add(item);
                AddItemChildrenRecursive(itemControl, item);
            }

            UserSettings.Instance.ChatItems = items;
            UserSettings.Instance.Save();

            CancelEdit();
        }

        private void AddItemChildrenRecursive(ChatItemControl parentControl, ChatItem parent)
        {
            if (parentControl.Children != null)
            {
                List<ChatItem> children = new List<ChatItem>();

                foreach (ChatItemControl itemControl in parentControl.Children)
                {
                    ChatItem item = new ChatItem(itemControl.Index, itemControl.Title);
                    children.Add(item);
                    AddItemChildrenRecursive(itemControl, item);
                }

                parent.Children = children;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            LoadItems();
            CancelEdit();
        }

        private void CancelEdit()
        {
            EditButton.Visibility = Visibility.Visible;
            PrintButton.Visibility = Visibility.Visible;
            SaveButton.Visibility = Visibility.Collapsed;
            CancelButton.Visibility = Visibility.Collapsed;

            foreach (ChatItemControl c in ItemsStackPanel.Children)
            {
                c.IsEditable = false;
            }
        }

        private void PrintButton_Click(object sender, RoutedEventArgs e)
        {
            string text = string.Empty;

            foreach (ChatItem chatItem in UserSettings.Instance.ChatItems)
            {
                text += chatItem.Index + ". " + chatItem.Title + Environment.NewLine;
                text += GetTextRecursive(chatItem, 1);
            }

            new ConfirmDialog(Window.GetWindow(this), text, "Export", false)
            {
                Width = 500,
                Height = 400,
                ResizeMode = ResizeMode.CanResize
            }.ShowDialog();
        }

        private string GetTextRecursive(ChatItem parent, int depth)
        {
            string text = string.Empty;

            if (parent.Children != null)
            {
                string prefix = string.Empty;
                for (int i = 0; i < depth; i++)
                {
                    prefix += "\t";
                }

                foreach (ChatItem chatItem in parent.Children)
                {
                    text += prefix + chatItem.Index + ". " + chatItem.Title + Environment.NewLine;
                    text += GetTextRecursive(chatItem, depth + 1);
                }
            }

            return text;
        }
    }
}
